package com.zhdl.modules.demo.dglProcess;


import com.zhdl.modules.demo.testprocess.ProcessOptInterface;
import com.zhdl.modules.process.BaseProcess;
import com.zhdl.modules.process.service.MainProcessValueProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 单隔离项目主流程控制
 */
@Component
@Slf4j
public class DGLMainProcess extends BaseProcess {
   @Resource
   DGLProcessInitService dglProcessInitService;
   @Resource
   MainProcessValueProvider mainProcessValueProvider;
   @Resource
   WorkPlace1Process workPlace1Process;
   @Resource
   WorkPlace2Process workPlace2Process;
   @Resource
   WorkPlace3Process workPlace3Process;
    @Override
    public void initProcessList() {
         processBeanList =  dglProcessInitService.initMainList();
    }
    public static int optWorkPlace = -1;

    @Override
    public void initHardware() {
        super.initHardware();
        desc = "主流程";
        initProcessList();
        childList.add(workPlace1Process);
        childList.add(workPlace2Process);
        childList.add(workPlace3Process);
        workPlace1Process.initHardware();
        workPlace2Process.initHardware();
        workPlace3Process.initHardware();
        mainProcessValueProvider.initData();
        statusHolder.initComplete = true;
        statusHolder.setAuto(true);
        VeryInterval = 5000;
        statusHolder.setOptInterface(new ProcessOptInterface() {
            @Override
            public void start() {
                if (optWorkPlace!=-1){
                    log.info("工位"+optWorkPlace+"start");
                    if (optWorkPlace == 1){
                        workPlace1Process.statusHolder.start();
                        optWorkPlace = -1;
                    } else if (optWorkPlace == 2) {
                        workPlace2Process.statusHolder.start();
                        optWorkPlace = -1;
                    } else if (optWorkPlace == 3) {
                        workPlace3Process.statusHolder.start();
                        optWorkPlace = -1;
                    }
                }else{
                    for (BaseProcess baseProcess : childList) {
                        if (baseProcess.statusHolder.process>0) {
                            baseProcess.statusHolder.start();
                        }
                    }
                }
            }

            @Override
            public void stop() {
                if (optWorkPlace!=-1){
                    log.info("工位"+optWorkPlace+"start");
                    if (optWorkPlace == 1){
                        workPlace1Process.statusHolder.stop();
                        optWorkPlace = -1;
                    } else if (optWorkPlace == 2) {
                        workPlace2Process.statusHolder.stop();
                        optWorkPlace = -1;
                    } else if (optWorkPlace == 3) {
                        workPlace3Process.statusHolder.stop();
                        optWorkPlace = -1;
                    }
                }else{
                    for (BaseProcess baseProcess : childList) {
                        if (baseProcess.statusHolder.process>0) {
                            baseProcess.statusHolder.stop();
                        }
                    }
                }
            }

            @Override
            public void release() {

            }

            @Override
            public void reset() {
                workPlace2Process.statusHolder.reset();;
                workPlace1Process.statusHolder.reset();
                workPlace3Process.statusHolder.reset();
            }

            @Override
            public void startProcess(Integer process) {
                DGLMainProcess.this.startProcess(process);
            }
        });
    }
}
